package com.example.database;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class EnrollActivity extends AppCompatActivity {
    Button enroll;
    Button btn_return;
    EditText enroll_password;
    EditText enroll_username;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.enroll);
        enroll=(Button)findViewById(R.id.btn_enroll);
        btn_return=(Button)findViewById(R.id.btn_return);
        enroll_password=(EditText) findViewById(R.id.et_enroll_password);
        enroll_username=(EditText) findViewById(R.id.et_enroll_username);
        enroll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String pd = enroll_password.getText().toString();
                String ue = enroll_username.getText().toString();

                if (!pd.isEmpty() & !ue.isEmpty()) {
                    Database helper = new Database(EnrollActivity.this);
                    User user = new User(ue, pd);
                    helper.insert(user);
                    Toast.makeText(EnrollActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.setClass(EnrollActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                else if(pd.isEmpty()&!ue.isEmpty()){
                    Toast.makeText(EnrollActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                }
                else if (!pd.isEmpty()&ue.isEmpty()){
                    Toast.makeText(EnrollActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(EnrollActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                }
            }
        });
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

}